package com.example.lemonhealth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.lemonhealth.constant.MessageConstant;
import com.example.lemonhealth.entity.TCheckgroup;
import com.example.lemonhealth.service.CheckgroupService;
import com.example.lemonhealth.utils.PageResult;
import com.example.lemonhealth.utils.QueryPageBean;
import com.example.lemonhealth.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author: Lemon
 * @Description: 检查组管理相关接口
 * @Date: 2024/12/24 16:24
 */
@Controller
@RequestMapping("/checkgroup")
@Slf4j
@Transactional
public class CheckgroupController {

    @Resource
    private CheckgroupService checkgroupService;

    /**
     * @Author Lemon
     * @Description 分页查询-检查组管理
     * @Date 2024/12/24 16:32
     * @Param [queryPageBean]
     * @Return com.example.lemonhealth.utils.PageResult
     */
    @PostMapping("/findPage.do")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        IPage<TCheckgroup> page = checkgroupService.findPage(queryPageBean);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * @Author Lemon
     * @Description 添加检查组
     * @Date 2024/12/24 17:57
     * @Param [checkitemIds, checkgroup]
     * @Return com.example.lemonhealth.utils.Result
     */
    @PostMapping("/add.do")
    @ResponseBody
    public Result addCheckgroup(@RequestParam String checkitemIds , @RequestBody TCheckgroup checkgroup) {
        boolean flag;
        String message;
        Object data;
        try {
            if (StringUtils.isNotEmpty(checkgroup.getName())){
                checkgroupService.addCheckgroup(checkitemIds, checkgroup);
                flag = true;
                message = MessageConstant.ADD_CHECKGROUP_SUCCESS;
                data = null;
            }else {
                flag = false;
                message = MessageConstant.ADD_CHECKGROUP_FAIL_ISNOTNULL;
                data = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
            message = MessageConstant.ADD_CHECKGROUP_FAIL;
            data = e.getMessage();
        }
        return new Result(flag,message,data);
    }

    /**
     * @Author Lemon
     * @Description 根据id查询检查组（数据回显）
     * @Date 2024/12/25 14:56
     * @Param [id]
     * @Return com.example.lemonhealth.utils.Result
     */
    @GetMapping("findById.do")
    @ResponseBody
    public Result findById(@RequestParam String id) {
        boolean flag;
        String message;
        Object data;
        try{
            if (StringUtils.isNotEmpty(id)){
                TCheckgroup tCheckgroup = checkgroupService.findById(id);
                flag = true;
                message = MessageConstant.QUERY_CHECKGROUP_SUCCESS;
                data = tCheckgroup;
            }else {
                flag = false;
                message = MessageConstant.QUERY_CHECKGROUP_FAIL;
                data = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            message = MessageConstant.QUERY_CHECKGROUP_FAIL;
            data = e.getMessage();
        }
        return new Result(flag,message,data);
    }

    /**
     * @Author Lemon
     * @Description 根据checkgroupId查询检查项（数据回显）
     * @Date 2024/12/25 15:32
     * @Param [id]
     * @Return com.example.lemonhealth.utils.Result
     */
    @GetMapping("findCheckItemIdsByCheckGroupId.do")
    @ResponseBody
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer id) {
        boolean flag;
        String message;
        Object data;
        try{
            if (Objects.nonNull(id)){
                List list = checkgroupService.findCheckItemIdsByCheckGroupId(id);
                flag = true;
                message = MessageConstant.QUERY_CHECKITEM_SUCCESS;
                data = list;
            }else {
                flag = false;
                message = MessageConstant.QUERY_CHECKITEM_FAIL;
                data = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
            message = MessageConstant.QUERY_CHECKITEM_FAIL;
            data = e.getMessage();
        }
        return new Result(flag,message,data);
    }

    /**
     * @Author Lemon
     * @Description 修改检查组
     * @Date 2024/12/25 17:28
     * @Param [checkitemIds, checkgroup]
     * @Return com.example.lemonhealth.utils.Result
     */
    @PostMapping("edit.do")
    @ResponseBody
    public Result editCheckgroup(@RequestParam String checkitemIds , @RequestBody TCheckgroup checkgroup) {
        boolean flag;
        String message;
        Object data;
        try{
            checkgroupService.editCheckgroup(checkitemIds,checkgroup);
            flag = true;
            message = MessageConstant.EDIT_CHECKGROUP_SUCCESS;
            data = checkgroup;
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
            message = MessageConstant.EDIT_CHECKGROUP_FAIL;
            data = e.getMessage();
        }
        return new Result(flag,message,data);
    }
}
